package com.roamer.javase.demo.export.vo;

import lombok.Data;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 导出模型
 *
 * @author roamer
 * @version v1.0
 * @date 2020/5/5 16:36
 */
@Data
public abstract class ExportModel implements Serializable {
    private static final long serialVersionUID = 6823903967246143133L;

    protected List<AbstractDataItem> dataItems = new ArrayList<>();

    /**
     * 追加元素
     *
     * @param dataItem 元素
     */
    public void appendDataItem(AbstractDataItem dataItem) {
        dataItems.add(dataItem);
    }

    /**
     * 设置跨行跨列
     * @param sheet
     */
    public void addMergedRegion(Sheet sheet) {
        List<int[]> list = getMergedRegion();
        for (int[] regions : list) {
            CellRangeAddress rangeAddress =  new CellRangeAddress(regions[0], regions[1], regions[2], regions[3]);
            if (rangeAddress.getNumberOfCells() >= 2){
                sheet.addMergedRegion(rangeAddress);
            }

        }

    }

    public abstract List<int[]> getMergedRegion();

    /**
     * 标题
     *
     * @return {@code String} 标题
     */
    public abstract String getExportTitle();


    /**
     * 表头别名
     *
     * @return {@code Map<String, String>} 表头别名
     */
    public abstract Map<String, String> getExportHeaderAlias();

    /**
     * 获取数据集
     *
     * @return {@code List<Map<String, Object>>}
     */
    public List<Map<String, Object>> getDataMapList() {
        if (getDataItems() == null || getDataItems().isEmpty()) {
            return Collections.emptyList();
        }
        return getDataItems().stream().map(AbstractDataItem::getDataMap).collect(Collectors.toList());
    }


    /**
     * 导出数据项
     */
    @Data
    public abstract static class AbstractDataItem implements Serializable {
        private static final long serialVersionUID = 2445132348160610991L;

        protected String project;

        protected Date date;

        protected LocalTime time;

        protected String deviceName;

        protected abstract Map<String, Object> getDataMap();
    }
}
